﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKRevitBase.WinForm;
using RevitInteractiveUtility.ExternalEventManager;

namespace YJKColumnModify.ConnectColumnBream
{
	public partial class GeneralCheckResultForm : System.Windows.Forms.Form
	{
		public GeneralCheckResultForm(Dictionary<string, List<ConnectColumnBreamInfo>> dicConnectColumnBreamInfo, WindowHandle hWndRevit)
		{
			this.m_hWndRevit = hWndRevit;
			this.InitExternalEventRequest();
			this.InitializeComponent();
			this.AddCheckBoxToTree(dicConnectColumnBreamInfo);
			this.treeView1.AfterCheck += this.treeView1_AfterCheck;
			this.treeView1.NodeMouseDoubleClick += this.treeView_TreeNodeDoubleMouseClickEventHandler;
			this.InitCheck();
		}

		private void AddCheckBoxToTree(Dictionary<string, List<ConnectColumnBreamInfo>> dicConnectColumnBreamInfo)
		{
			GeneralCheckResultForm.m_dicTreeNodeOldState.Clear();
			foreach (KeyValuePair<string, List<ConnectColumnBreamInfo>> keyValuePair in dicConnectColumnBreamInfo)
			{
				int num = 0;
				TreeNode[] array = new TreeNode[keyValuePair.Value.Count];
				foreach (ConnectColumnBreamInfo connectColumnBreamInfo in keyValuePair.Value)
				{
					TreeNode treeNode = new TreeNode("待处理" + (num + 1).ToString());
					this.m_commandData = connectColumnBreamInfo.ConnectBreamInfo.ThisExternalCommandData;
					treeNode.Text = "待处理" + (num + 1).ToString();
					treeNode.Checked = true;
					array[num] = treeNode;
					num++;
					this.m_dicTreeNodeToBream.Add(treeNode, connectColumnBreamInfo);
					GeneralCheckResultForm.m_dicTreeNodeOldState.Add(treeNode, treeNode.Checked);
				}
				TreeNode treeNode2 = new TreeNode(keyValuePair.Key + "( " + keyValuePair.Value.Count.ToString() + "处待处理)", array);
				treeNode2.Checked = true;
				this.treeView1.Nodes.AddRange(new TreeNode[]
				{
					treeNode2
				});
			}
		}

		private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
		{
			if (e.Action != TreeViewAction.ByMouse)
			{
				return;
			}
			TreeNode node = e.Node;
			if (node.Text == "未处理的检查结果")
			{
				return;
			}
			List<ConnectColumnBreamInfo> list = new List<ConnectColumnBreamInfo>();
			List<ConnectColumnBreamInfo> list2 = new List<ConnectColumnBreamInfo>();
			if (node.Nodes.Count > 0)
			{
				TreeNodeCollection nodes = node.Nodes;
				if (node.Checked)
				{
					IEnumerator enumerator = nodes.GetEnumerator();
					{
						while (enumerator.MoveNext())
						{
							object obj = enumerator.Current;
							TreeNode key = obj as TreeNode;
							if (!GeneralCheckResultForm.m_dicTreeNodeOldState[key])
							{
								list.Add(this.m_dicTreeNodeToBream[key]);
								GeneralCheckResultForm.m_dicTreeNodeOldState[key] = true;
							}
						}
						goto IL_1C4;
					}
				}
				IEnumerator enumerator3 = nodes.GetEnumerator();
				{
					while (enumerator3.MoveNext())
					{
						object obj2 = enumerator3.Current;
						TreeNode key2 = obj2 as TreeNode;
						if (GeneralCheckResultForm.m_dicTreeNodeOldState[key2])
						{
							list2.Add(this.m_dicTreeNodeToBream[key2]);
							GeneralCheckResultForm.m_dicTreeNodeOldState[key2] = false;
						}
					}
					goto IL_1C4;
				}
			}
			TreeNode parent = node.Parent;
			bool @checked = false;
			IEnumerator enumerator2 = parent.Nodes.GetEnumerator();
			{
				while (enumerator2.MoveNext())
				{
					if ((enumerator2.Current as TreeNode).Checked)
					{
						@checked = true;
						break;
					}
				}
			}
			parent.Checked = @checked;
			if (node.Checked)
			{
				list.Add(this.m_dicTreeNodeToBream[node]);
				GeneralCheckResultForm.m_dicTreeNodeOldState[node] = true;
			}
			else
			{
				list2.Add(this.m_dicTreeNodeToBream[node]);
				GeneralCheckResultForm.m_dicTreeNodeOldState[node] = false;
			}
			IL_1C4:
			if (list.Count > 0)
			{
				ExternalEventManagerSingleton.GetEventManger(false).DoRequest(this.m_iRequestAddId, this.m_commandData.Application, list);
			}
			if (list2.Count > 0)
			{
				ExternalEventManagerSingleton.GetEventManger(false).DoRequest(this.m_iRequestDeleteId, this.m_commandData.Application, list2);
			}
		}

		private void treeView_TreeNodeDoubleMouseClickEventHandler(object sender, TreeNodeMouseClickEventArgs e)
		{
			TreeNode node = e.Node;
			if (node != null && this.m_dicTreeNodeToBream.ContainsKey(node))
			{
				ConnectColumnBreamInfo connectColumnBreamInfo = this.m_dicTreeNodeToBream[node];
				new ViewDefining(connectColumnBreamInfo.ConnectBreamInfo.ThisExternalCommandData).CreateViewDefining(connectColumnBreamInfo);
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			foreach (KeyValuePair<TreeNode, ConnectColumnBreamInfo> keyValuePair in this.m_dicTreeNodeToBream)
			{
				if (keyValuePair.Key.Checked)
				{
					this.m_litSelectInfo.Add(keyValuePair.Value);
				}
			}
			ExternalEventManagerSingleton.GetEventManger(false).DoRequest(this.m_iRequestNewBreamId, this.m_commandData.Application, this.m_litSelectInfo);
			this.bDeleteDetailCurve = true;
			base.Close();
		}

		private void GeneralCheckResultForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void InitExternalEventRequest()
		{
			ExternalEventManagerSingleton eventManger = ExternalEventManagerSingleton.GetEventManger(false);
			this.m_upDataDeleteDetailCurve = new UpDataDeleteDetailCurveReqst(this.m_iRequestDeleteId);
			this.m_NewBreamReqst = new NewBreamReqst(this.m_iRequestNewBreamId);
			this.m_upDataAddDetailCurve = new UpDataAddDetailCurveReqst(this.m_iRequestAddId);
			eventManger.AppendRequest(this.m_upDataDeleteDetailCurve);
			eventManger.AppendRequest(this.m_NewBreamReqst);
			eventManger.AppendRequest(this.m_upDataAddDetailCurve);
		}

		private void GeneralCheckResultForm_FormClosed(object sender, FormClosedEventArgs e)
		{
		}

		private void GeneralCheckResultForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.bDeleteDetailCurve)
			{
				return;
			}
			List<ConnectColumnBreamInfo> list = new List<ConnectColumnBreamInfo>();
			foreach (KeyValuePair<TreeNode, ConnectColumnBreamInfo> keyValuePair in this.m_dicTreeNodeToBream)
			{
				if (keyValuePair.Value.ArrayDetailCurve != null)
				{
					list.Add(keyValuePair.Value);
				}
			}
			ExternalEventManagerSingleton.GetEventManger(false).DoRequest(this.m_iRequestDeleteId, this.m_commandData.Application, list);
		}

		private void GeneralCheckResultForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void InitCheck()
		{
			foreach (object obj in this.treeView1.Nodes)
			{
				(obj as TreeNode).Checked = false;
			}
			foreach (object obj2 in this.treeView1.Nodes)
			{
				(obj2 as TreeNode).Checked = true;
			}
		}

		private WindowHandle m_hWndRevit;

		private Dictionary<TreeNode, ConnectColumnBreamInfo> m_dicTreeNodeToBream = new Dictionary<TreeNode, ConnectColumnBreamInfo>();

		private static Dictionary<TreeNode, bool> m_dicTreeNodeOldState = new Dictionary<TreeNode, bool>();

		private ExternalCommandData m_commandData;

		private int m_iRequestDeleteId = 1;

		private int m_iRequestAddId = 2;

		private int m_iRequestNewBreamId = 3;

		private UpDataDeleteDetailCurveReqst m_upDataDeleteDetailCurve;

		private UpDataAddDetailCurveReqst m_upDataAddDetailCurve;

		private NewBreamReqst m_NewBreamReqst;

		private List<ConnectColumnBreamInfo> m_litSelectInfo = new List<ConnectColumnBreamInfo>();

		private bool bDeleteDetailCurve;
	}
}
